package com.ttdevs.es;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

import com.ttdevs.es.service.MainService;
import com.ttdevs.es.service.MainService.ServieBinder;
import com.ttdevs.util.CheckLicense;
import com.ttdevs.util.DialogUtil;
import com.ttdevs.util.StaticConstants;

public class StartHandler extends Handler implements StaticConstants {
	
	private CheckLicense checkLicense;
	
	private AlertDialog dialog;
	private Context context;
	private Intent serviceIntent;

	private boolean isBound;

	public StartHandler(Context context, Context serviceContext) {
		this.context = context;

		serviceIntent = new Intent(context, MainService.class);
		serviceContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
		
		checkLicense = new CheckLicense(context, this);
		checkLicense.check();
	}

	public void handleMessage(Message msg) {
		switch (msg.what) {
		case ERROR:
			System.out.println(msg.obj);
			dialog = DialogUtil.getSubmitDialog(context, msg.obj.toString(), null);
			dialog.show();
			break;
		case PASS:
			System.out.println(msg.obj);
			dialog = DialogUtil.getNormalDialog(context, "倒计时 " + DELAYSTART + " 秒", submitListener, cancelListener);
			dialog.show();
			countDownTimer.start();
			break;
		case NORIGHT:
			
			break;

		default:
			break;
		}
	}

	private CountDownTimer countDownTimer = new CountDownTimer(DELAYSTART * 1000, 1000) {

		@Override
		public void onFinish() {
			dialog.setTitle("倒计时结束，开始启动...");
			dialog.dismiss();
			startMonitor();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			System.out.println("millisUntilFinished:" + millisUntilFinished);
			dialog.setTitle("倒计时 " + String.valueOf(millisUntilFinished / 1000) + " 秒");
		}

	};

	private DialogInterface.OnClickListener submitListener = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			countDownTimer.cancel();
			startMonitor();
		}
	};
	private DialogInterface.OnClickListener cancelListener = new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			countDownTimer.cancel();
		}
	};

	private void startMonitor() {
		if (isBound) {
			mainService.start();

			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.addCategory(Intent.CATEGORY_HOME);
			context.startActivity(intent);
		} else {
			Toast.makeText(context, "系统初始化异常", Toast.LENGTH_LONG).show();
		}
	}

	// ********************************************************
	private MainService mainService;
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			isBound = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			ServieBinder binder = (ServieBinder) service;
			mainService = binder.getService();
			isBound = true;
		}
	};
}